package com.aopcinemas.visitor.ui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.aopcinemas.common.model.Person;
import com.aopcinemas.visitor.VisitorAgent;
import com.aopcinemas.visitor.behaviour.VisitorRegistrationBehaviour;
import com.toedter.calendar.JDateChooser;

public class VisitorRegistrationDialog extends JDialog {

    private static final long serialVersionUID = 1L;

    private JTextField uid = new JTextField(10);
    private JTextField name = new JTextField(10);
    private JDateChooser dateOfBirth = new JDateChooser();
    private JComboBox language = new JComboBox(new String[] { "RUS", "ENG", "EST" });
    private JTextField email = new JTextField(10);

    private VisitorAgent agent;
    private JFrame owner;

    public VisitorRegistrationDialog(JFrame frame, VisitorAgent agent) {
        super(frame, true);
        this.owner = frame;
        this.agent = agent;

        JPanel panel = new JPanel();

        GridBagLayout layout = new GridBagLayout();
        GridBagConstraints constraints = new GridBagConstraints();
        constraints.insets = new Insets(3, 3, 3, 3);
        panel.setLayout(layout);

        JLabel label = new JLabel("UID: ");
        constraints.gridwidth = 1;
        constraints.anchor = GridBagConstraints.EAST;
        layout.setConstraints(label, constraints);
        panel.add(label);

        constraints.gridwidth = GridBagConstraints.REMAINDER;
        constraints.anchor = GridBagConstraints.WEST;
        layout.setConstraints(uid, constraints);
        panel.add(uid);

        label = new JLabel("Name: ");
        constraints.gridwidth = 1;
        constraints.anchor = GridBagConstraints.EAST;
        layout.setConstraints(label, constraints);
        panel.add(label);

        constraints.gridwidth = GridBagConstraints.REMAINDER;
        constraints.anchor = GridBagConstraints.WEST;
        layout.setConstraints(name, constraints);
        panel.add(name);

        label = new JLabel("Date of birth: ");
        constraints.gridwidth = 1;
        constraints.anchor = GridBagConstraints.EAST;
        layout.setConstraints(label, constraints);
        panel.add(label);

        constraints.gridwidth = GridBagConstraints.REMAINDER;
        constraints.anchor = GridBagConstraints.WEST;
        layout.setConstraints(dateOfBirth, constraints);
        panel.add(dateOfBirth);

        label = new JLabel("Language: ");
        constraints.gridwidth = 1;
        constraints.anchor = GridBagConstraints.EAST;
        layout.setConstraints(label, constraints);
        panel.add(label);

        constraints.gridwidth = GridBagConstraints.REMAINDER;
        constraints.anchor = GridBagConstraints.WEST;
        layout.setConstraints(language, constraints);
        panel.add(language);

        label = new JLabel("E-mail: ");
        constraints.gridwidth = 1;
        constraints.anchor = GridBagConstraints.EAST;
        layout.setConstraints(label, constraints);
        panel.add(label);

        constraints.gridwidth = GridBagConstraints.REMAINDER;
        constraints.anchor = GridBagConstraints.WEST;
        layout.setConstraints(email, constraints);
        panel.add(email);

        JPanel buttonPanel = new JPanel();

        JButton button = new JButton("Sign in");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                Person person = new Person();
                person.setUid(uid.getText());
                person.setName(name.getText());
                person.setDateOfBirth(dateOfBirth.getDate());
                person.setEmail(email.getText());
                person.setLanguage(language.getSelectedItem().toString());
                VisitorRegistrationDialog.this.agent.setPerson(person);
                VisitorRegistrationDialog.this.agent.addBehaviour(new VisitorRegistrationBehaviour());
                VisitorRegistrationDialog.this.setVisible(false);
                VisitorRegistrationDialog.this.owner.setLocationRelativeTo(null);
                VisitorRegistrationDialog.this.owner.setVisible(true);
            }
        });
        buttonPanel.add(button);

        JButton exit = new JButton("Exit");
        exit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent evt) {
                VisitorRegistrationDialog.this.agent.doDelete();
                owner.setVisible(false);
            }
        });
        buttonPanel.add(exit);

        constraints.gridwidth = GridBagConstraints.REMAINDER;
        constraints.anchor = GridBagConstraints.EAST;
        layout.setConstraints(buttonPanel, constraints);
        panel.add(buttonPanel);

        getContentPane().add(panel);

        setSize(new Dimension(250, 250));
        setResizable(false);
        setDefaultCloseOperation(HIDE_ON_CLOSE);
    }
}
